package com.ycsj.goldmedalnewconcept.teacher.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.base.BusInfo;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.master.bean.ListBean;
import com.ycsj.goldmedalnewconcept.master.bean.MsgInfo;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 教师备课
 * Created by Administrator on 2019/5/6 0006.
 */

public class TeacherPrepareLessonsActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.lv_jxmb)
    ImageView lvJxmb;
    @BindView(R.id.lv_znd)
    ImageView lvZnd;
    @BindView(R.id.tv_fs)
    TextView tvFs;
    @BindView(R.id.bt_fs)
    Button btFs;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;

    //Tab页面选中的资源图片集合
    private int[] tabSelectIds = {R.drawable.jxmb_select, R.drawable.znd_select};

    //Tab页面未选中的资源图片集合
    private int[] tabUnselectIds = {R.drawable.jxmb_normal, R.drawable.znd_normal};

    //跳转携带的数据对象
    private ListBean info;

    //班级号
    private String classnum;

    //重难点和教学目标
    private String difficultyMsg, teachTarget;

    /**
     * 跳转当前界面的方法
     *
     * @param context
     * @param info
     */
    public static void goActivity(Context context, ListBean info, String classnum) {
        Intent it = new Intent(context, TeacherPrepareLessonsActivity.class);
        it.putExtra("classnum", classnum);
        Bundle bundle = new Bundle();
        bundle.putSerializable("info", info);
        it.putExtras(bundle);
        context.startActivity(it);
    }


    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_teacher_prepare_lessons;
    }

    @Override
    protected void init() {
        initStateBar();

        //获取跳转数据
        initIntent();

        //初始化标题
        initTitle();

        //请求数据
        httpDiffer();
    }

    /**
     * 请求页面数据,先请求重难点数据,在请求教学目标数据
     */
    private void httpDiffer() {
        //请求中难点数据
        Map<String, String> map = new HashMap<>();
        map.put("type", "16");
        map.put("unit", info.unit);

        HttpUtils.post(HttpAddress.ZNDMSG, map, this, new HttpCallBack<MsgInfo>() {
            @Override
            public void onSuccess(MsgInfo msgInfo) {


                //记录下
                difficultyMsg = msgInfo.content;

                //请求教学目标的数据
                httpTeacherTarget();
            }

            @Override
            public void onError(String e) {
                rlLoading.setVisibility(View.GONE);
                ToastUtils.showShort(BaseApplication.ERROR);
            }
        });
    }

    /**
     * 请求教学目标的数据
     */
    private void httpTeacherTarget() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "17");
        map.put("unit", info.unit);

        HttpUtils.post(HttpAddress.ZNDMSG, map, this, new HttpCallBack<MsgInfo>() {
            @Override
            public void onSuccess(MsgInfo msgInfo) {
                rlLoading.setVisibility(View.GONE);
                //记录下教学目标
                teachTarget = msgInfo.content;
                llContent.setVisibility(View.VISIBLE);

                //设置页面数据,默认显示教学目标
                tvFs.setText(Html.fromHtml(teachTarget));
            }

            @Override
            public void onError(String e) {
                rlLoading.setVisibility(View.GONE);
                ToastUtils.showShort(BaseApplication.ERROR);
            }
        });
    }

    /**
     * 获取跳转数据
     */
    private void initIntent() {
        info = (ListBean) getIntent().getSerializableExtra("info");
        classnum = getIntent().getStringExtra("classnum");
    }

    /**
     * 初始化标题栏
     */
    private void initTitle() {
        tvTitle.setText("教学与反思");
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_back, R.id.lv_jxmb, R.id.lv_znd, R.id.bt_fs})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.lv_jxmb:
                //教学目标
                lvJxmb.setImageResource(tabSelectIds[0]);
                lvZnd.setImageResource(tabUnselectIds[1]);
                tvFs.setText(Html.fromHtml(teachTarget));
                //设置文字
                break;
            case R.id.lv_znd:
                //重难点
                lvJxmb.setImageResource(tabUnselectIds[0]);
                lvZnd.setImageResource(tabSelectIds[1]);
                tvFs.setText(Html.fromHtml(difficultyMsg));
                //设置文字
                break;
            case R.id.bt_fs:
                //查看教学与反思详情
                TeacherPrepareLessonDetailsActivity.goActivity(TeacherPrepareLessonsActivity.this, info.content, info.unit, classnum, info.row_id);
                break;
        }
    }

    /**
     * 事件处理
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
        if (null != busInfo && TeacherChooseUnitActivity.UPDATESTATE == busInfo.type) {
            //当老师发布新反思时更新状态
            info.content = busInfo.msg;
            info.hasFansi = "1";
            if (busInfo.updateRowId == 110) {
                //更新一下当前页面的对象的Row_Id
                info.row_id = busInfo.row_id;
            }
        }
    }
}

